package com.mingqijia.gassafety.shared.utils.commonUtils;

import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *
 * @author wanghai
 * @since 2022-02-22
 */


public class DateUtils {
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String TIME_FORMAT = "HH:mm:ss";
    public static final String YEAR_FORMAT = "yyyy";
    public static final String DATE_CHINESE_FORMAT = "yyyy年MM月dd日";
    public static final String WEEK = "EEEE";
    public static final String DATE_SHORT_FORMAT = "yyyyMMdd";
    public static final String DATE_TIME_LONG_FORMAT = "yyyyMMddHHmmss";
    public static final String DATE_TIME_SLASH_FORMAT = "yyyy/MM/dd HH:mm:ss";
    public static final String DATE_SLASH_FORMAT = "yyyy/MM/dd";
    public static long ONE_DAY_SECONDS = 86400L;
    public static long ONE_DAY_MILLISECONDS = 86400000L;
    public static final String DATE_DAY_FORMAT_SHORT = "yyMMdd";

    public DateUtils(){}

    public static Timestamp getTimestamp(Date date) {
        return new Timestamp(date.getTime());
    }

    public static String datetimeFormatNow() {
        return getFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

    public static String datetimeallFormatNow() {
        return getFormat("yyyy/MM/dd/HH/mm/ss").format(new Date());
    }

    public static Date now() {
        return new Date();
    }

    public static String nowHourMinStr() {
        Date date = now();
        return getFormat("HH:mm").format(date);
    }

    public static Date nowMin() {
        Date date = now();
        String s = date == null ? "" : getFormat("yyyy-MM-dd HH:mm").format(date);

        try {
            date = toDateTimeMin(s);
        } catch (Exception var3) {
        }

        return date;
    }
    public static Date today() {
        Date date = now();
        String s = dateFormat(date);

        try {
            date = toDate(s);
        } catch (Exception var3) {
        }

        return date;
    }

    public static final Date toShortDate(String date) throws ParseException {
        return StringUtils.isBlank(date) ? null : getFormat("yyyyMMdd").parse(date);
    }

    public static final Date toDate(String strDate) throws ParseException {
        return StringUtils.isBlank(strDate) ? null : getFormat("yyyy-MM-dd").parse(strDate);
    }

    public static final Date toDate(String strDate, String format){
        try {
            return StringUtils.isBlank(strDate) ? null : getFormat(format).parse(strDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static final Date toDateTime(String strDateTime) throws ParseException {
        return StringUtils.isBlank(strDateTime) ? null : getFormat("yyyy-MM-dd HH:mm:ss").parse(strDateTime);
    }

    public static final Date toDateTimeMin(String strDateTime) throws ParseException {
        return StringUtils.isBlank(strDateTime) ? null : getFormat("yyyy-MM-dd HH:mm").parse(strDateTime);
    }

    public static final Date toLongDateTime(String longDateTime) throws ParseException {
        return StringUtils.isBlank(longDateTime) ? null : getFormat("yyyyMMddHHmmss").parse(longDateTime);
    }

    public static final String longToSlashDateTime(String longDateTime) throws ParseException {
        return StringUtils.isBlank(longDateTime) ? getFormat("yyyy/MM/dd HH:mm:ss").format(new Date()) : getFormat("yyyy/MM/dd HH:mm:ss").format(toLongDateTime(longDateTime));
    }

    public static final DateFormat getFormat(String format) {
        return new SimpleDateFormat(format);
    }

    public static final String datetimeFormat(Date date) {
        return date == null ? "" : getFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    public static final String dateFormat(Date date) {
        return date == null ? "" : getFormat("yyyy-MM-dd").format(date);
    }

    public static final String dateFormat(Date date, String format) {
        return date == null ? "" : getFormat(format).format(date);
    }

    public static final String dateTimeSlashFormat(Date date) {
        return date == null ? "" : getFormat("yyyy/MM/dd HH:mm:ss").format(date);
    }

    public static final String dateSlashFormat(Date date) {
        return date == null ? "" : getFormat("yyyy/MM/dd").format(date);
    }

    public static final String format(Date date, String formate) {
        return date == null ? "" : getFormat(formate).format(date);
    }

    public static final Date strToDtSimpleFormat(String strDate) {
        if (strDate == null) {
            return null;
        } else {
            try {
                return getFormat("yyyy-MM-dd").parse(strDate);
            } catch (Exception var2) {
                return null;
            }
        }
    }

    public static final String getDiffDate(Date dt, int idiff) {
        Calendar c = Calendar.getInstance();
        c.setTime(dt);
        c.add(5, idiff);
        return dateFormat(c.getTime());
    }

    public static final String getDiffYear(Date dt, int idiff) {
        Calendar c = Calendar.getInstance();
        c.setTime(dt);
        c.add(Calendar.YEAR, idiff);
        return dateFormat(c.getTime());
    }

    public static final Date getDiffYearDate(Date dt, int idiff) {
        Calendar c = Calendar.getInstance();
        c.setTime(dt);
        c.add(Calendar.YEAR, idiff);
        return c.getTime();
    }

    public static final String getDiffMon(Date dt, int idiff) {
        Calendar c = Calendar.getInstance();
        c.setTime(dt);
        c.add(2, idiff);
        return dateFormat(c.getTime());
    }

    public static final Date getDiffMonData(Date dt, int idiff) {
        Calendar c = Calendar.getInstance();
        c.setTime(dt);
        c.add(2, idiff);
        return c.getTime();
    }

    public static Date getDayBegin(Date date) {
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        df.setLenient(false);
        String dateString = df.format(date);

        try {
            return df.parse(dateString);
        } catch (ParseException var4) {
            return date;
        }
    }

    public static boolean isOneDayZeroPoint(Date date) {
        if (date != null) {
            long time = date.getTime() + ONE_DAY_MILLISECONDS / 3L;
            if (time % ONE_DAY_MILLISECONDS == 0L) {
                return true;
            }
        }

        return false;
    }

    public static final String dateChineseFormat(Date date) {
        return date == null ? "" : getFormat("yyyy年MM月dd日").format(date);
    }

    public static final String dtSimpleChineseFormatStr(String date) throws ParseException {
        return date == null ? "" : getFormat("yyyy年MM月dd日").format(toDate(date));
    }

    public static final Date string2DateTimeByAutoZero(String stringDate) throws ParseException {
        if (stringDate == null) {
            return null;
        } else {
            if (stringDate.length() == 11) {
                stringDate = stringDate + "00:00:00";
            } else if (stringDate.length() == 13) {
                stringDate = stringDate + ":00:00";
            } else if (stringDate.length() == 16) {
                stringDate = stringDate + ":00";
            } else if (stringDate.length() == 10) {
                stringDate = stringDate + " 00:00:00";
            }

            return getFormat("yyyy-MM-dd HH:mm:ss").parse(stringDate);
        }
    }

    public static final Date string2DateTimeBy23(String stringDate) throws ParseException {
        if (stringDate == null) {
            return null;
        } else {
            if (stringDate.length() == 11) {
                stringDate = stringDate + "23:59:59";
            } else if (stringDate.length() == 13) {
                stringDate = stringDate + ":59:59";
            } else if (stringDate.length() == 16) {
                stringDate = stringDate + ":59";
            } else if (stringDate.length() == 10) {
                stringDate = stringDate + " 23:59:59";
            }

            return getFormat("yyyy-MM-dd HH:mm:ss").parse(stringDate);
        }
    }

    public static final Date dateTimeTo23(Date date) {
        if (date == null) {
            return null;
        } else {
            date.setHours(23);
            date.setMinutes(59);
            date.setSeconds(59);
            return date;
        }
    }

    public static final Date dateTimeToZero(Date date) {
        return date == null ? null : strToDtSimpleFormat(dateFormat(date));
    }

    public static final int calculateDecreaseDate(String beforDate, String afterDate) throws ParseException {
        Date date1 = getFormat("yyyy-MM-dd").parse(beforDate);
        Date date2 = getFormat("yyyy-MM-dd").parse(afterDate);
        return (int)getDateBetween(date1, date2) / 1000 / 3600 / 24;
    }

    public static int getDiffDays2(Date one, Date two) {
        Calendar sysDate = new GregorianCalendar();
        sysDate.setTime(one);
        Calendar failDate = new GregorianCalendar();
        failDate.setTime(two);
        return (int)((sysDate.getTimeInMillis() - failDate.getTimeInMillis()) / 86400000L);
    }

    public static final long getDateBetween(Date dBefor, Date dAfter) {
        long lBefor = 0L;
        long lAfter = 0L;
        long lRtn = 0L;
        lBefor = dBefor.getTime();
        lAfter = dAfter.getTime();
        lRtn = lAfter - lBefor;
        return lRtn;
    }

    public static final Date shortstring2Date(String stringDate) throws ParseException {
        return stringDate == null ? null : getFormat("yyyyMMdd").parse(stringDate);
    }

    public static final String shortStringToString(String stringDate) throws ParseException {
        return stringDate == null ? null : shortDate(strToDtSimpleFormat(stringDate));
    }

    public static final String shortDate(Date date) {
        return date == null ? null : getFormat("yyyyMMdd").format(date);
    }

    public static final String longDateTime(Date date) {
        return date == null ? null : getFormat("yyyyMMddHHmmss").format(date);
    }

    public static final Long string2DateLong(String stringDate) throws ParseException {
        Date d = toDate(stringDate);
        return d == null ? null : d.getTime();
    }

    public static final String hmsFormat(Date date) {
        return date == null ? "" : getFormat("HH:mm:ss").format(date);
    }

    public static final String getDiffDate(int diff) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(5, diff);
        return dateFormat(c.getTime());
    }

    public static Date getTomorrowAMDate() {
        Date tomorrowDate = increaseDate(now(), 1);
        return dateTimeToZero(tomorrowDate);
    }

    public static final Date getDiffDateTime(int diff) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(5, diff);
        return c.getTime();
    }

    public static final String getDiffDate(String srcDate, String format, int diff) {
        SimpleDateFormat f = new SimpleDateFormat(format);

        try {
            Date source = f.parse(srcDate);
            Calendar c = Calendar.getInstance();
            c.setTime(source);
            c.add(5, diff);
            return f.format(c.getTime());
        } catch (Exception var6) {
            return srcDate;
        }
    }

    public static String getBeforeDay(String stringDate) throws ParseException {
        Date tempDate = toDate(stringDate);
        Calendar cad = Calendar.getInstance();
        cad.setTime(tempDate);
        cad.add(5, -1);
        return dateFormat(cad.getTime());
    }

    public static String getBeforeDay(Date date) throws ParseException {
        Calendar cad = Calendar.getInstance();
        cad.setTime(date);
        cad.add(5, -1);
        return dateFormat(cad.getTime());
    }

    public static String getBeforeDate(Date date, int day) {
        Calendar cad = Calendar.getInstance();
        cad.setTime(date);
        cad.add(5, -day);
        return dateFormat(cad.getTime());
    }

    public static Date getBeforeDate_(Date date, int day) {
        Calendar cad = Calendar.getInstance();
        cad.setTime(date);
        cad.add(5, -day);
        return cad.getTime();
    }

    public static Date getAfterDate(Date date, int day) {
        if (null == date) {
            date = new Date();
        }

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(5, day);
        date = c.getTime();
        return date;
    }

    public static String getBeforeHour(Date date, int hour) {
        Calendar cad = Calendar.getInstance();
        cad.setTime(date);
        cad.set(11, cad.get(11) - hour);
        return datetimeFormat(cad.getTime());
    }

    public static String getBeforeMinute(Date date, int minute) {
        Calendar cad = Calendar.getInstance();
        cad.setTime(date);
        cad.set(12, cad.get(12) - minute);
        return datetimeFormat(cad.getTime());
    }

    public static Long getBeforeMinute(int minute) {
        Calendar cad = Calendar.getInstance();
        cad.setTime(new Date());
        cad.set(12, cad.get(12) - minute);
        return cad.getTime().getTime() / 1000L;
    }

    public static String getBeforeSecond(int second) {
        Calendar cad = Calendar.getInstance();
        cad.setTime(new Date());
        cad.set(13, cad.get(13) - second);
        return getFormat("yyyy-MM-dd HH:mm:ss").format(cad.getTime());
    }

    public static final String getDiffDateTime(int diff, int hours) {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(5, diff);
        c.add(10, hours);
        return dateFormat(c.getTime());
    }

    public static final Long dateToNumber(Date date) {
        if (date == null) {
            return null;
        } else {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            String month;
            if (c.get(2) + 1 >= 10) {
                month = "" + (c.get(2) + 1);
            } else {
                month = "0" + (c.get(2) + 1);
            }

            String day;
            if (c.get(5) >= 10) {
                day = "" + c.get(5);
            } else {
                day = "0" + c.get(5);
            }

            String number = c.get(1) + "" + month + day;
            return new Long(number);
        }
    }

    public static Map<String, String> getLastWeek(String stringDate, int interval) throws ParseException {
        Map<String, String> lastWeek = new HashMap();
        Date tempDate = shortstring2Date(stringDate);
        Calendar cad = Calendar.getInstance();
        cad.setTime(tempDate);
        int dayOfMonth = cad.getActualMaximum(5);
        cad.add(5, dayOfMonth - 1);
        lastWeek.put("endDate", shortDate(cad.getTime()));
        cad.add(5, interval);
        lastWeek.put("startDate", shortDate(cad.getTime()));
        return lastWeek;
    }

    public static String getNextMon(String stringDate) throws ParseException {
        Date tempDate = shortstring2Date(stringDate);
        Calendar cad = Calendar.getInstance();
        cad.setTime(tempDate);
        cad.add(2, 1);
        return shortDate(cad.getTime());
    }

    public static String getNextDay(String stringDate) throws ParseException {
        Date tempDate = toDate(stringDate);
        Calendar cad = Calendar.getInstance();
        cad.setTime(tempDate);
        cad.add(5, 1);
        return dateFormat(cad.getTime());
    }

    public static String getNextDay(Date date) throws ParseException {
        Calendar cad = Calendar.getInstance();
        cad.setTime(date);
        cad.add(5, 1);
        return dateFormat(cad.getTime());
    }

    public static Date getNextDayDtShort(String stringDate) throws ParseException {
        Date tempDate = shortstring2Date(stringDate);
        Calendar cad = Calendar.getInstance();
        cad.setTime(tempDate);
        cad.add(5, 1);
        return cad.getTime();
    }

    public static long countDays(String startDate, String endDate) {
        Date tempDate1 = null;
        Date tempDate2 = null;
        long days = 0L;

        try {
            tempDate1 = toDate(startDate);
            tempDate2 = toDate(endDate);
            days = (tempDate2.getTime() - tempDate1.getTime()) / 86400000L;
            return days;
        } catch (ParseException var7) {
            return 0L;
        }
    }

    public static Date addMonth(Date date, int month) {
        Date retDate = (Date)date.clone();
        retDate.setMonth(retDate.getMonth() + month);
        return retDate;
    }

    public static Date addDays(Date date1, long days) {
        return addSeconds(date1, days * ONE_DAY_SECONDS);
    }

    public static Date addSeconds(Date date1, long secs) {
        return new Date(date1.getTime() + secs * 1000L);
    }

    public static int countDays(Date dateStart, Date dateEnd) {
        return dateStart != null && dateEnd != null ? (int)((dateEnd.getTime() - dateStart.getTime()) / 86400000L) : -1;
    }

    public static boolean checkDays(Date start, Date end, int days) {
        int g = countDays(start, end);
        return g <= days;
    }

    public static String getDiffStringDate(Date dt, int diff) {
        Calendar ca = Calendar.getInstance();
        if (dt == null) {
            ca.setTime(new Date());
        } else {
            ca.setTime(dt);
        }

        ca.add(5, diff);
        return dateFormat(ca.getTime());
    }

    public static boolean checkTime(String statTime) {
        if (statTime.length() > 8) {
            return false;
        } else {
            String[] timeArray = statTime.split(":");
            if (timeArray.length != 3) {
                return false;
            } else {
                for(int i = 0; i < timeArray.length; ++i) {
                    String tmpStr = timeArray[i];

                    try {
                        Integer tmpInt = new Integer(tmpStr);
                        if (i == 0) {
                            if (tmpInt > 23 || tmpInt < 0) {
                                return false;
                            }
                        } else if (tmpInt > 59 || tmpInt < 0) {
                            return false;
                        }
                    } catch (Exception var5) {
                        return false;
                    }
                }

                return true;
            }
        }
    }

    public static final String stringToStringDate(String stringDate) {
        if (StringUtils.isBlank(stringDate)) {
            return null;
        } else {
            return stringDate.length() != 8 ? null : stringDate.substring(0, 4) + stringDate.substring(4, 6) + stringDate.substring(6, 8);
        }
    }

    public static Date string2Date(String str, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);

        try {
            return sdf.parse(str);
        } catch (ParseException var4) {
            return null;
        }
    }

    public static final Date increaseDate(Date aDate, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(aDate);
        cal.add(5, days);
        return cal.getTime();
    }

    public static final Date increaseDateByHour(Date aDate, int hour) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(aDate);
        cal.add(11, hour);
        return cal.getTime();
    }

    public static final boolean isLeapYear(int year) {
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }

    public static final boolean isDefaultWorkingDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int week = calendar.get(7);
        return week != 7 && week != 1;
    }

    public static final List<Date> getAllDates(int year) {
        Date firstDay = new Date(year - 1900, 0, 1);
        int dayCount = 365;
        if (isLeapYear(year)) {
            dayCount = 366;
        }

        List<Date> dateList = new ArrayList();

        for(int i = 1; i <= dayCount; ++i) {
            dateList.add(increaseDate(firstDay, i - 1));
        }

        return dateList;
    }

    public static final List<Date> getAllDates(int year, int month) {
        Date firstDay = new Date(year - 1900, month, 1);
        int dayCount = 30;
        if (month != 1 && month != 3 && month != 5 && month != 7 && month != 8 && month != 10 && month != 12) {
            if (month == 2) {
                if (isLeapYear(year)) {
                    dayCount = 29;
                } else {
                    dayCount = 28;
                }
            }
        } else {
            dayCount = 31;
        }

        List<Date> dateList = new ArrayList();

        for(int i = 1; i <= dayCount; ++i) {
            dateList.add(increaseDate(firstDay, i - 1));
        }

        return dateList;
    }

    public static final Date newDate(int year, int month, int day) {
        return new Date(year - 1900, month - 1, day);
    }

    public static final String getWeekDay(Date date) {
        return getFormat("EEEE").format(date);
    }

    public static final Date getCurrentYearMonthDay() {
        Date date = new Date();
        return new Date(date.getYear(), date.getMonth(), date.getDate());
    }

    public static Date parseDateNoTime(String sDate, String format) throws ParseException {
        if (StringUtils.isBlank(format)) {
            throw new ParseException("Null format. ", 0);
        } else {
            DateFormat dateFormat = new SimpleDateFormat(format);
            if (sDate != null && sDate.length() >= format.length()) {
                return dateFormat.parse(sDate);
            } else {
                throw new ParseException("length too little", 0);
            }
        }
    }

    public static final Date getNowDateForPageSelectBehindDate() {
        Calendar cal = Calendar.getInstance();
        if (cal.get(12) < 30) {
            cal.set(12, 30);
        } else {
            cal.set(11, cal.get(11) + 1);
            cal.set(12, 0);
        }

        return cal.getTime();
    }

    public static final Date getNowDateForPageSelectAheadDate() {
        Calendar cal = Calendar.getInstance();
        if (cal.get(12) < 30) {
            cal.set(12, 0);
        } else {
            cal.set(12, 30);
        }

        return cal.getTime();
    }

    public static Date[] getWeekStartEnd(Date date) {
        if (date == null) {
            date = now();
        }

        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int weekDay = cal.get(7);
        Date startDate = increaseDate(date, -weekDay + 2);
        Date endDate = increaseDate(date, 8 - weekDay);
        return new Date[]{new Date(startDate.getYear(), startDate.getMonth(), startDate.getDate()), new Date(endDate.getYear(), endDate.getMonth(), endDate.getDate(), 23, 59)};
    }

    public static void initDateStart(Date date) {
        if (date != null) {
            date.setHours(0);
            date.setMinutes(0);
            date.setSeconds(0);
        }
    }

    public static void initDateEnd(Date date) {
        if (date != null) {
            date.setHours(23);
            date.setMinutes(59);
            date.setSeconds(59);
        }
    }

    public static String formatDateString(String date) {
        String result = "";
        if (StringUtils.isBlank(date)) {
            return "";
        } else {
            if (date.length() == 10) {
                result = date.substring(0, 4) + date.substring(5, 7) + date.substring(8, 10);
            }

            return result;
        }
    }

    public static int getDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(7);
    }

    public static String getCurMonthFirstDay() {
        Calendar cal = Calendar.getInstance();
        Date d = new Date();
        cal.setTime(d);
        cal.set(5, 1);
        return dateFormat(cal.getTime());
    }

    public static String getCurMonthFirstDay(String date) {
        Date d = strToDtSimpleFormat(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.set(5, 1);
        return dateFormat(cal.getTime());
    }

    public static String getCurSeasonFirstDay() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        int season = cal.get(2);
        if (season >= 0 && season <= 2) {
            cal.set(2, 0);
        }

        if (season >= 3 && season <= 5) {
            cal.set(2, 3);
        }

        if (season >= 6 && season <= 8) {
            cal.set(2, 6);
        }

        if (season >= 9 && season <= 11) {
            cal.set(2, 9);
        }

        cal.set(5, 1);
        return dateFormat(cal.getTime());
    }

    public static String getCurSeasonFirstDay(String date) {
        Date d = strToDtSimpleFormat(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        int season = cal.get(2);
        if (season >= 0 && season <= 2) {
            cal.set(2, 0);
        }

        if (season >= 3 && season <= 5) {
            cal.set(2, 3);
        }

        if (season >= 6 && season <= 8) {
            cal.set(2, 6);
        }

        if (season >= 9 && season <= 11) {
            cal.set(2, 9);
        }

        cal.set(5, 1);
        return dateFormat(cal.getTime());
    }

    public static String getNextMonthFirstDay(String date) {
        Calendar cal = Calendar.getInstance();
        Date dt = strToDtSimpleFormat(date);
        cal.setTime(dt);
        cal.add(2, 1);
        cal.set(5, 1);
        return dateFormat(cal.getTime());
    }

    public static String getMonday(String date) {
        Date d = strToDtSimpleFormat(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.set(7, 2);
        return dateFormat(cal.getTime());
    }

    public static String getMondayEx(String date) {
        Date d = strToDtSimpleFormat(date);
        Calendar cal = Calendar.getInstance();
        cal.setFirstDayOfWeek(2);
        cal.setTime(d);
        cal.set(7, 2);
        return dateFormat(cal.getTime());
    }

    public static boolean isWeekend(String date) {
        Calendar cal = Calendar.getInstance();
        Date d = strToDtSimpleFormat(date);
        cal.setTime(d);
        int day = cal.get(7);
        return day == 1 || day == 7;
    }

    public static String getSunday(String date) {
        Date d = strToDtSimpleFormat(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(3, 1);
        cal.set(7, 1);
        return dateFormat(cal.getTime());
    }

    public static String getThursday(String date) {
        Date d = strToDtSimpleFormat(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.set(7, 5);
        return dateFormat(cal.getTime());
    }

    public static String getPreviousFriday(String date) {
        Date d = strToDtSimpleFormat(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        cal.add(3, -1);
        cal.set(7, 6);
        return dateFormat(cal.getTime());
    }

    public static Date getPreviousMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int month = calendar.get(2);
        int year = calendar.get(1);
        if (month == 0) {
            --year;
            month = 11;
        } else {
            --month;
        }

        calendar.set(1, year);
        calendar.set(2, month);
        calendar.set(5, 1);
        return calendar.getTime();
    }

    public static Date getPreviousMonthLastDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(2, -1);
        calendar.set(5, 1);
        calendar.roll(5, -1);
        return calendar.getTime();
    }

    public static int getDayNum() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(5);
    }

    public static Date getDateFromDayNum(int dayNum) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(5, dayNum);
        return calendar.getTime();
    }

    public static String getYearString() {
        return getFormat("yyyy").format(new Date());
    }

    public static boolean checkBetween(Date startDate, Date endDate) {
        return getDateBetween(startDate, new Date()) >= 0L && getDateBetween(new Date(), endDate) >= 0L;
    }

    public static String getPreviouMonthFirstDay(String format) {
        return format != null && !format.equals("") && !format.equals(" ") ? getFormat(format).format(getPreviousMonthFirstDay()) : getFormat("yyyy-MM-dd hh:mm:ss").format(getPreviousMonthFirstDay());
    }

    public static String getPreviouMonthLastDay(String format) {
        return format != null && !format.equals("") && !format.equals(" ") ? getFormat(format).format(getPreviousMonthLastDay()) : getFormat("yyyy-MM-dd hh:mm:ss").format(getPreviousMonthLastDay());
    }

    public static Date getTodayBeforeThisMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int year = calendar.get(1);
        int month = calendar.get(2);
        int day = calendar.get(5);
        if (month == 0) {
            --year;
            month = 11;
        } else {
            --month;
        }

        calendar.set(year, month, day, 0, 0, 0);
        return calendar.getTime();
    }

    public static Date getTodayLastSecond() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int year = calendar.get(1);
        int month = calendar.get(2);
        int day = calendar.get(5);
        calendar.set(year, month, day, 23, 59, 59);
        return calendar.getTime();
    }

    public static String getTodayBeforeThisMonth(String format) {
        return getFormat(format).format(getTodayBeforeThisMonth());
    }

    public static String getTodayLastSecond(String format) {
        return getFormat(format).format(getTodayLastSecond());
    }

    public static final Date adjustToWee(Date date) {
        Calendar calender = Calendar.getInstance();
        calender.setTime(date);
        calender.set(11, 0);
        calender.set(12, 0);
        calender.set(13, 0);
        calender.set(14, 0);
        return calender.getTime();
    }

    public static final Date adjust(Date date, int h, int m, int s) {
        Calendar calender = Calendar.getInstance();
        calender.setTime(date);
        calender.set(11, h);
        calender.set(12, m);
        calender.set(13, s);
        return calender.getTime();
    }

    public static final boolean diffMoreThan(Date one, Date two, int monthCount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(two);
        int year = calendar.get(1);
        int month = calendar.get(2);
        int day = calendar.get(5);
        int hour = calendar.get(11);
        int minute = calendar.get(12);
        int second = calendar.get(13);
        if (month - monthCount < 0) {
            --year;
            month = 11 + (month - monthCount) + 1;
        } else {
            month -= monthCount;
        }

        calendar.set(year, month, day, hour, minute, second);
        Date before = calendar.getTime();
        return getDateBetween(one, before) <= 0L;
    }

    public static Date addMonthsAndDays(Date date, int months, int days) {
        Calendar sysDate = new GregorianCalendar();
        sysDate.setTime(date);
        sysDate.add(2, months);
        sysDate.add(5, days);
        return sysDate.getTime();
    }

    public static boolean isSameDay(Date date1, Date date2) {
        DateFormat fmt = getFormat("yyyy-MM-dd");
        return fmt.format(date1).equals(fmt.format(date2));
    }


    public static Date addDateOneDay(Date date) {
        if (null == date) {
            return date;
        } else {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(5, 1);
            date = c.getTime();
            return date;
        }
    }

    public static boolean isDateTime(String dateTime) {
        try {
            Date dt = toDateTime(dateTime);
            return dt != null;
        } catch (Exception var2) {
            return false;
        }
    }

    public static List<Date> findDurationDates(String startDate, String endDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dBegin = sdf.parse(startDate.replaceAll("/", "-"));
        Date dEnd = sdf.parse(endDate.replaceAll("/", "-"));
        List<Date> lDate = new ArrayList();
        lDate.add(dBegin);
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(dEnd);

        while(dEnd.after(calBegin.getTime())) {
            calBegin.add(5, 1);
            lDate.add(calBegin.getTime());
        }

        return lDate;
    }

    public static Long dateTimeDuration(Date dateStart, Date dateEnd) {
        long timeDuration = dateEnd.getTime() - dateStart.getTime();
        return timeDuration / 1000L;
    }

    public static boolean isTimeout(Date dateStart, Date dateEnd, Long timeoutSeconds) {
        Long dateDuration = dateTimeDuration(dateStart, dateEnd);
        boolean isTimeout = dateDuration > timeoutSeconds;
        return isTimeout;
    }

    public static boolean checkTimeBetween(String timeStart, String timeEnd) throws ParseException {
        String startDateStr = dateFormat(new Date()) + " " + timeStart;
        String endDateStr = dateFormat(new Date()) + " " + timeEnd;
        Date startDate = toDateTime(startDateStr);
        Date endDate = toDateTime(endDateStr);
        return checkBetween(startDate, endDate);
    }

    public static String dateDayFormatNow() {
        return getFormat("yyMMdd").format(new Date());
    }

    public static Date getCurrMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        Date d = new Date();
        calendar.setTime(d);
        calendar.set(5, 1);
        return calendar.getTime();
    }

    public static Date getCurrMonthLastDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(2, 1);
        calendar.set(5, 1);
        calendar.roll(5, -1);
        return calendar.getTime();
    }

    public static String getCurrMonthFirstDaySlashFormat() {
        Calendar calendar = Calendar.getInstance();
        Date d = new Date();
        calendar.setTime(d);
        calendar.set(5, 1);
        return dateSlashFormat(calendar.getTime());
    }

    public static String getCurrMonthLastDaySlashFormat() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(5, 1);
        calendar.roll(5, -1);
        return dateSlashFormat(calendar.getTime());
    }

    public static final String format(Long dateTmie, String formate) {
        Date date = new Date(dateTmie);
        return getFormat(formate).format(date);
    }

    public static Timestamp getAfterDays(Date date, int days) {
        long time = date.getTime();
        time += ONE_DAY_MILLISECONDS * (long)days;
        Timestamp timestamp = new Timestamp(time);
        return timestamp;
    }

    public static String toEnglishDateStr(Date date) {
        return (new SimpleDateFormat("d-MMM-yy", Locale.ENGLISH)).format(date).toUpperCase();
    }

    public static Date millisToDate(Long millis) {
        Calendar cad = Calendar.getInstance();
        cad.setTimeInMillis(millis);
        return cad.getTime();
    }

    public static String millisToDateString(Long millis) {
        Date date = millisToDate(millis);
        return getFormat("yyyy-MM-dd HH:mm:ss").format(date);
    }

    public static long stringToLong(String dateString) {
        Date date = null;

        try {
            date = toDateTime(dateString);
        } catch (ParseException var3) {
            var3.printStackTrace();
        }

        return date.getTime();
    }

    public static Date formatDateStandard(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;

        try {
            date = sdf.parse(dateStr);
        } catch (ParseException var4) {
            var4.printStackTrace();
        }

        return date;
    }

    public static Date getAfterMinute(Date date, int minute) {
        Calendar cad = Calendar.getInstance();
        cad.setTime(date);
        cad.set(12, cad.get(12) + minute);
        return cad.getTime();
    }

    public static Long getLongSecond() {
        return System.currentTimeMillis() / 1000L;
    }
}


